package com.mdd.product.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mdd.product.entity.ProductSku;
import com.mdd.product.mapper.ProductSkuMapper;
import com.mdd.product.service.IProductSkuService;
import com.mdd.product.utils.sku.SpecAlgorithm;
import com.mdd.product.validate.ProductSkuCreateValidate;
import com.mdd.product.validate.ProductSkuUpdateValidate;
import com.mdd.product.vo.CreateSkulVo;
import com.mdd.product.vo.ProductPropertyDetailVo;
import com.mdd.product.vo.ProductSkuListedVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import javax.annotation.Resource;
import java.util.*;

/**
 * 商品sku实现类
 * @author LikeAdmin
 */
@Service
public class ProductSkuServiceImpl implements IProductSkuService {

    @Resource
    ProductSkuMapper productSkuMapper;

    /**
     * 商品sku列表
     *
     * @author LikeAdmin
     * @return PageResult<ProductSkuListedVo>
     */
    @Override
    public List<ProductSkuListedVo> list(Long spuId) {
        LambdaQueryWrapper<ProductSku> productWrapper=new LambdaQueryWrapper<>();
        productWrapper.eq(ProductSku::getSpuId,spuId);
        List<ProductSku> data = productSkuMapper.selectList(productWrapper);

        List<ProductSkuListedVo> list = new LinkedList<>();
        for(ProductSku item : data) {
            ProductSkuListedVo vo = new ProductSkuListedVo();
            BeanUtils.copyProperties(item, vo);
            list.add(vo);
        }
        return list;
    }

    @Override
    public List<ProductSkuListedVo> createSkuOld(List<ProductPropertyDetailVo> list) {
        List<List<String>> arrList=new ArrayList<>();
        list.forEach(productPropertyDetailVo -> {
            List<String> arrValue=new ArrayList<>();
            productPropertyDetailVo.getPropertyValues()
                    .forEach(productPropertyValueDetailVo -> arrValue.add(productPropertyValueDetailVo.getName()));
            arrList.add(arrValue);
        });
        List<ProductSkuListedVo> productSkuListedVos=new ArrayList<>();
        List<String> data= new SpecAlgorithm().generateSkuCombine(arrList, 0, "", new ArrayList<String>());
        data.forEach(s -> {
            ProductSkuListedVo productSkuListedVo=new ProductSkuListedVo();
            productSkuListedVo.setProperties(s);
            productSkuListedVos.add(productSkuListedVo);
        });
        return productSkuListedVos;
    }

    @Override
    public List<ProductSkuListedVo> createSku(CreateSkulVo createSkulVo) {
        List<ProductPropertyDetailVo> list  = createSkulVo.getProductPropertys();
        List<List<String>> arrList=new ArrayList<>();
        list.forEach(productPropertyDetailVo -> {
            List<String> arrValue=new ArrayList<>();
            productPropertyDetailVo.getPropertyValues()
                    .forEach(productPropertyValueDetailVo -> arrValue.add(productPropertyDetailVo.getName()+":"+productPropertyValueDetailVo.getName()));
            arrList.add(arrValue);
        });
        List<ProductSkuListedVo> productSkuListedVos=new ArrayList<>();
        List<String> data= new SpecAlgorithm().generateSkuCombine(arrList, 0, "", new ArrayList<String>());
        data.forEach(s -> {
            ProductSkuListedVo productSkuListedVo=new ProductSkuListedVo();
            productSkuListedVo.setProperties(s);
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("properties",s);
            if(Objects.nonNull(createSkulVo.getSpuId())) {
                queryWrapper.eq("spu_id", createSkulVo.getSpuId());
                ProductSku productSku = productSkuMapper.selectOne(queryWrapper);
                if (Objects.nonNull(productSku)) {
                    BeanUtils.copyProperties(productSku, productSkuListedVo);
                }
            }
            productSkuListedVos.add(productSkuListedVo);
        });
        return productSkuListedVos;
    }

    /**
     * 商品sku新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Transactional
    @Override
    public void add(ProductSkuCreateValidate createValidate) {
        ProductSku model = new ProductSku();
        model.setSpuId(createValidate.getSpuId());
        model.setProperties(createValidate.getProperties());
        model.setPrice(createValidate.getPrice());
        model.setMarketPrice(createValidate.getMarketPrice());
        model.setCostPrice(createValidate.getCostPrice());
        model.setPicUrl(createValidate.getPicUrl());
        model.setSalesCount(createValidate.getSalesCount());
        productSkuMapper.insert(model);
    }

    /**
     * 商品sku编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Transactional
    @Override
    public void edit(ProductSkuUpdateValidate updateValidate) {
        ProductSku model = productSkuMapper.selectById(updateValidate.getId());

        Assert.notNull(model, "商品sku数据不存在!");

        model.setId(updateValidate.getId());
        model.setSpuId(updateValidate.getSpuId());
        model.setProperties(JSON.toJSONString(updateValidate.getProperties()));
        model.setPrice(updateValidate.getPrice());
        model.setMarketPrice(updateValidate.getMarketPrice());
        model.setCostPrice(updateValidate.getCostPrice());
        model.setPicUrl(updateValidate.getPicUrl());
        model.setSalesCount(updateValidate.getSalesCount());
        productSkuMapper.updateById(model);
    }

    /**
     * 商品sku删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Transactional
    @Override
    public void del(Long id) {
        ProductSku model = productSkuMapper.selectById(id);

        Assert.notNull(model, "商品sku数据不存在!");

        productSkuMapper.deleteById(id);
    }
    @Transactional
    @Override
    public void delSpu(Long spuId) {
        LambdaQueryWrapper<ProductSku> sku=new LambdaQueryWrapper<>();
        sku.eq(ProductSku::getSpuId,spuId);
        productSkuMapper.delete(sku);
    }
    @Transactional
    @Override
    public void delSkuBySpuIdAndSku(Long spuId, Set<Long> existSkuId) {
        LambdaQueryWrapper<ProductSku> sku=new LambdaQueryWrapper<>();
        sku.eq(ProductSku::getSpuId,spuId);
        if(!existSkuId.isEmpty()) {
            sku.notIn(ProductSku::getId, existSkuId);
        }
        productSkuMapper.delete(sku);
    }

}
